// 设备管理状态管理
import { defineStore } from 'pinia'

export const useDeviceStore = defineStore('device', {
  state: () => ({
    // 设备列表
    deviceList: [],
    
    // 当前选中的设备
    currentDevice: null,
    
    // 设备状态统计
    deviceStatusStats: {
      online: 0,
      offline: 0,
      fault: 0,
      total: 0
    },
    
    // 设备分组
    deviceGroups: [],
    
    // 设备维护记录
    maintenanceRecords: [],
    
    // 加载状态
    loading: false,
    
    // 错误信息
    error: null
  }),

  getters: {
    // 获取在线设备
    getOnlineDevices: (state) => {
      return state.deviceList.filter(device => device.status === 1)
    },
    
    // 获取离线设备
    getOfflineDevices: (state) => {
      return state.deviceList.filter(device => device.status === 2)
    },
    
    // 获取故障设备
    getFaultDevices: (state) => {
      return state.deviceList.filter(device => device.status === 3)
    },
    
    // 获取设备状态统计
    getDeviceStatusStats: (state) => {
      const stats = {
        online: 0,
        offline: 0,
        fault: 0,
        total: state.deviceList.length
      }
      
      state.deviceList.forEach(device => {
        switch (device.status) {
          case 1:
            stats.online++
            break
          case 2:
            stats.offline++
            break
          case 3:
            stats.fault++
            break
        }
      })
      
      return stats
    },
    
    // 获取设备状态颜色
    getDeviceStatusColor: (state) => (status) => {
      const colorMap = {
        1: '#4cd964', // 在线 - 绿色
        2: '#999999', // 离线 - 灰色
        3: '#dd524d'  // 故障 - 红色
      }
      return colorMap[status] || '#999999'
    },
    
    // 获取设备状态文本
    getDeviceStatusText: (state) => (status) => {
      const textMap = {
        1: '在线',
        2: '离线',
        3: '故障'
      }
      return textMap[status] || '未知'
    }
  },

  actions: {
    // 设置设备列表
    setDeviceList(devices) {
      this.deviceList = devices
    },
    
    // 添加设备
    addDevice(device) {
      this.deviceList.unshift(device)
    },
    
    // 更新设备
    updateDevice(deviceId, updates) {
      const index = this.deviceList.findIndex(device => device.deviceId === deviceId)
      if (index !== -1) {
        this.deviceList[index] = { ...this.deviceList[index], ...updates }
      }
    },
    
    // 删除设备
    removeDevice(deviceId) {
      const index = this.deviceList.findIndex(device => device.deviceId === deviceId)
      if (index !== -1) {
        this.deviceList.splice(index, 1)
      }
    },
    
    // 设置当前设备
    setCurrentDevice(device) {
      this.currentDevice = device
    },
    
    // 设置设备分组
    setDeviceGroups(groups) {
      this.deviceGroups = groups
    },
    
    // 设置维护记录
    setMaintenanceRecords(records) {
      this.maintenanceRecords = records
    },
    
    // 添加维护记录
    addMaintenanceRecord(record) {
      this.maintenanceRecords.unshift(record)
    },
    
    // 更新设备状态
    updateDeviceStatus(deviceId, status) {
      const device = this.deviceList.find(d => d.deviceId === deviceId)
      if (device) {
        device.status = status
      }
    },
    
    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },
    
    // 设置错误信息
    setError(error) {
      this.error = error
    },
    
    // 清除错误信息
    clearError() {
      this.error = null
    }
  }
})
